package com.hrbxlh.task;

import cn.hutool.core.date.DateUtil;
import com.hrbxlh.dao.MemberDao;
import com.hrbxlh.virtualization.support.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * @author sy
 */
public class DelOverDueMemberListTask implements Delayed, Runnable {

    private Logger logger = LoggerFactory.getLogger(DelOverDueMemberListTask.class);

    private static MemberDao memberDao = SpringContextHolder.getBean(MemberDao.class);

    private String stuorteaId;

    private long overdueTime;

    public DelOverDueMemberListTask(String stuorteaId, long overdueTime) {
        this.stuorteaId = stuorteaId;
        this.overdueTime = overdueTime;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        long a = overdueTime - DateUtil.currentSeconds();
        logger.info("差值："+a);
        return unit.convert(overdueTime - DateUtil.currentSeconds(), TimeUnit.SECONDS);
    }

    @Override
    public int compareTo(Delayed o) {
        if (o == this) {
            return 0;
        }
        if (o instanceof DelOverDueMemberListTask) {
            DelOverDueMemberListTask other = (DelOverDueMemberListTask) o;
            if (overdueTime < other.overdueTime) {
                return -1;
            } else if (overdueTime > other.overdueTime) {
                return 1;
            }
        }
        return 0;
    }

    @Override
    public void run() {

        try {
            List<String> delList = new ArrayList();
            delList.add(stuorteaId);
            memberDao.delMember(delList);
        }catch (Exception e){
            logger.error("定时删除过期时间报错",e);
        }


    }
}
